package cn.hunqing.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.hunqing.web.dto.RestResponseWrapper;
import cn.hunqing.web.msg.FileUploadMsgs;
import cn.hunqing.web.service.UploadedFileService;
import cn.hunqing.web.util.PathConstants;
 
/**
 * 
 * <p class="detail">
 * 功能：公共Action
 * </p>
 * 
 * @ClassName: BaseAction
 * @version V1.0
 * @date 2014年9月25日
 * @author wangsheng
 */
@Controller
public class FileUploadController {
	private static final Logger LOG = LoggerFactory
			.getLogger(FileUploadController.class.getName());

    @Autowired
    private UploadedFileService uploadedFileService;
    
    private String allowSuffix = "jpg,png,gif,jpeg";//允许文件格式
    private long allowSize = 2L;//允许文件大小
    private String fileName;
    private String[] fileNames;
     
    public String getAllowSuffix() {
        return allowSuffix;
    }
 
    public void setAllowSuffix(String allowSuffix) {
        this.allowSuffix = allowSuffix;
    }
 
    public long getAllowSize() {
        return allowSize*1024*1024;
    }
 
    public void setAllowSize(long allowSize) {
        this.allowSize = allowSize;
    }
 
    public String getFileName() {
        return fileName;
    }
 
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
 
    public String[] getFileNames() {
        return fileNames;
    }
 
    public void setFileNames(String[] fileNames) {
        this.fileNames = fileNames;
    }

	@RequestMapping(value=PathConstants.PATH_UPLOAD, method=RequestMethod.POST)
	public @ResponseBody RestResponseWrapper upload(MultipartHttpServletRequest request,Model model) {
		try {
			Map<String, MultipartFile> maps = request.getFileMap();

			if(maps.size() < 1){
				return RestResponseWrapper.error(FileUploadMsgs.EMPTY_UPLOAD_FILE);
			}
			
			if(maps.size() > 1){
				return RestResponseWrapper.error(FileUploadMsgs.MORE_ONE_UPLOAD_FILE);
			}
			
			String uplodFileId = null;
			for(String key:maps.keySet()){
				MultipartFile multipartFile = maps.get(key);
				checkUploadedFile(multipartFile);
				uplodFileId = uploadedFileService.upload(multipartFile);
				break;
			}
			
			LOG.debug("Upload successfully");
			return RestResponseWrapper.ok(uplodFileId);
		} catch (Exception e) {
			return RestResponseWrapper.error(e.getMessage());
		}
	}
	
	@RequestMapping(value="/uploadByBase64String")
	public @ResponseBody RestResponseWrapper upload(String base64String){
		String _base64String = base64String.replace("data:image/jpeg;base64,", "");
		try {
			// Base64解码
			byte[] b = Base64.decodeBase64(_base64String);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			String uplodFileId = uploadedFileService.upload(b, new Date().getTime() + ".jpeg");
			return RestResponseWrapper.ok(uplodFileId);
		} catch (Exception e) {
			return RestResponseWrapper.error(e.getMessage());
		}
	}
	
	private void checkUploadedFile(MultipartFile multipartFile){
		
	}
}